﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1.BLL;
using System.IO;

namespace TransactionCategoriser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private bool CheckCSVValidity(bool invaliddata)
        {
            foreach (string[] transactionData in parsedData)
            {
                if (transactionData.Count() > 8)
                {
                    MessageBox.Show("there's an extra comma in the file somewhere!!!!! line" + parsedData.IndexOf(transactionData));
                    invaliddata = true;
                }
            }
            return invaliddata;
        }

        private void ImportCSVData()
        {
            foreach (string[] transactionData in parsedData)
            {
                string sdate = transactionData[0];
                string sAmount = transactionData[5];
                if (string.IsNullOrEmpty(sAmount))
                {
                    sAmount = "-" + transactionData[6];
                }
                Transaction oTransaction = new Transaction();
                if (!oTransaction.Exists(Convert.ToDateTime(transactionData[0]), Convert.ToDecimal(sAmount), transactionData[4], transactionData[2], Convert.ToInt32(transactionData[3]), transactionData[1]))
                {
                    oTransaction.Add(Convert.ToDateTime(transactionData[0]), Convert.ToDecimal(sAmount), transactionData[4], transactionData[2], Convert.ToInt32(transactionData[3]), transactionData[1]);
                }
                else
                {
                   // MessageBox.Show("Transaction already exists");
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            CSVParser oCSVParser = new CSVParser();
            parsedData = oCSVParser.parseCSV(openFileDialog1.FileName);
            bool invaliddata = false;
            invaliddata = CheckCSVValidity(invaliddata);
            if (!invaliddata)
            {
                ImportCSVData();
            }
        }

       private List<string[]> parsedData = new List<string[]>();


        private void button2_Click(object sender, EventArgs e)
        {
            Form2 oForm2 = new Form2();
            oForm2.Show();
        }

        private void btnAnalyse_Click(object sender, EventArgs e)
        {
            frmAnalyse ofrmAnalyse = new frmAnalyse();
            ofrmAnalyse.Show();
        }
    }
}
